<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_Layer_Image_constructor (t) {
        t.plan( 13 );
        
        var options = { chicken: 151, foo: "bar", projection: "EPSG:4326" };
        var layer = new OpenLayers.Layer.Image('Test Layer',
                                        'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif',
                                                                        new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
                                                                                                        new OpenLayers.Size(580, 288), options);
        
        t.ok( layer instanceof OpenLayers.Layer.Image, "new OpenLayers.Layer.Image returns object" );
        t.eq( layer.CLASS_NAME, "OpenLayers.Layer.Image", "CLASS_NAME variable set correctly");

        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.id != null, "Layer is given an id");
        t.eq( layer.projection.getCode(), "EPSG:4326", "default layer projection correctly set");
        t.ok( ((layer.chicken == 151) && (layer.foo == "bar")), "layer.options correctly set to Layer Object" );
        t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly backed up" );

        options.chicken = 552;
        
        t.eq( layer.options["chicken"], 151 , "layer.options correctly made fresh copy" );
        
        t.eq( layer.isBaseLayer, true, "Default img layer is base layer" );

        layer = new OpenLayers.Layer.Image('Test Layer',
                                        'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif',
                                                                        new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
                                                                                                        new OpenLayers.Size(580, 288));
        t.ok( layer instanceof OpenLayers.Layer.Image, "new OpenLayers.Layer.Image returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.projection == null, "default layer projection correctly set");
        t.ok( layer.options instanceof Object, "layer.options correctly initialized as a non-null Object" );
    }
    
    function test_maxExtent(t) {
        t.plan(2);
        var layer;
        
        // test that the image extent is set as the default maxExtent
        var extent = new OpenLayers.Bounds(1, 2, 3, 4);
        var size = new OpenLayers.Size(2, 2);
        layer = new OpenLayers.Layer.Image("Test", "foo", extent, size);
        t.eq(layer.maxExtent.toString(), extent.toString(), "extent set as default maxExtent");
        
        // test that the maxExtent can be set explicitly
        var maxExtent = new OpenLayers.Bounds(10, 20, 30, 40);
        layer = new OpenLayers.Layer.Image("Test", "foo", extent, size, {maxExtent: maxExtent});
        t.eq(layer.maxExtent.toString(), maxExtent.toString(), "maxExtent can be set explicitly");
        
    }

    function test_Layer_Image_tileTests (t) {
        t.plan(7);
        var map = new OpenLayers.Map('map');
        
        layer = new OpenLayers.Layer.Image('Test Layer', 
                                        'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif',
                                                                        new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
                                                                                                        new OpenLayers.Size(580, 288));

        map.addLayer(layer);
        map.zoomToMaxExtent();

        // no resolution info was sent, so maxResolution should be calculated
        // by aspectRatio*extent/size (this is the pixel aspect ratio)
        var aspectRatio =  (layer.extent.getHeight() / layer.size.h) /
                           (layer.extent.getWidth() / layer.size.w);
        t.eq(aspectRatio, layer.aspectRatio, "aspectRatio is properly set");
        var maxExtent = aspectRatio * layer.extent.getWidth() / layer.size.w;
        t.eq(maxExtent, layer.maxResolution, "maxResolution is properly set");

        t.eq(layer.tile.position.x,-42, "Tile x positioned correctly at maxextent");
        t.eq(layer.tile.position.y,106, "Tile y positioned correctly at maxextent");
        t.eq(layer.tile.url, "http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif", "URL is correct");
        map.zoomIn();
        t.eq(layer.tile.url, "http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif", "URL is correct");
        layer.setUrl('http://labs.metacarta.com/wms/vmap0?LAYERS=basic&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=image%2Fjpeg&SRS=EPSG%3A4326&BBOX=-180,-90,0,90&WIDTH=256&HEIGHT=256');
        t.eq(layer.tile.url, "http://labs.metacarta.com/wms/vmap0?LAYERS=basic&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=image%2Fjpeg&SRS=EPSG%3A4326&BBOX=-180,-90,0,90&WIDTH=256&HEIGHT=256", "URL is correct after setURL");
    }
/******
 * 
 * 
 * HERE IS WHERE SOME TESTS SHOULD BE PUT TO CHECK ON THE LONLAT-PX TRANSLATION
 * FUNCTIONS AND RESOLUTION AND GETEXTENT GETZOOMLEVEL, ETC
 * 
 * 
 */


    function test_Layer_Image_destroy_before_use (t) {
        t.plan(1);
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.Image('Test', 'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif', new OpenLayers.Bounds(-180, -88.759, 180, 88.759), new OpenLayers.Size(580, 288));
        map.addLayer(layer);
        map.removeLayer(layer);
        layer.destroy();
        t.ok(true, "destroy() didn't throw an error");
    }

    function test_Layer_Image_destroy (t) {
        t.plan( 4 );    

        var map = new OpenLayers.Map('map');
        
        layer = new OpenLayers.Layer.Image('Test Layer', 
                                        'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif',
                                                                        new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
                                                                                                        new OpenLayers.Size(580, 288));

        map.addLayer(layer);
        map.zoomToMaxExtent();
    
        layer.destroy();

        t.eq( layer.name, null, "layer.name is null after destroy" );
        t.eq( layer.div, null, "layer.div is null after destroy" );
        t.eq( layer.map, null, "layer.map is null after destroy" );
        t.eq( layer.options, null, "layer.options is null after destroy" );

    }

    function test_loadEvents(t) {
        t.plan(3);

        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.Image(
            'Test', '../../img/blank.gif',
            new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
            new OpenLayers.Size(580, 288)
        );
    
        map.addLayer(layer);

        layer.events.register('loadstart', null, function(obj) {
            t.ok(obj.object.tile.isLoading, "loadstart triggered while tile is loading");
        });

        var delay = false;
        layer.events.register('loadend', null, function(obj) {
            delay = true;
        });

        t.delay_call(5, function() {
            t.eq(delay, true, "registered for loadend");
            t.eq(layer.tile.isLoading, false, "loadend triggered after tile is loaded");
            map.destroy(); //tear down
            return delay;
        });
        map.zoomToMaxExtent();
    }

  </script>
</head>
<body>
  <div id="map" style="width:500px;height:500px"></div>
  <div id="map2" style="width:100px;height:100px"></div>
</body>
</html>
